| Conditions | 2 |
| Paths | 2 |
| Total Lines | 35 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher'; |
||
| 11 | static getMenuItem(schema) { |
||
| 12 | if (!this.isAvailable(schema)) { |
||
| 13 | throw new Error('Footnote not available in schema!'); |
||
| 14 | } |
||
| 15 | return new MenuItem({ |
||
| 16 | command: (state, dispatch) => { |
||
| 17 | const { $from } = state.selection; |
||
| 18 | |||
| 19 | const index = $from.index(); |
||
| 20 | if (!$from.parent.canReplaceWith(index, index, schema.nodes.footnote)) { |
||
| 21 | return false; |
||
| 22 | } |
||
| 23 | |||
| 24 | const selectedContent = state.selection.content().content.toJSON(); |
||
| 25 | const footnoteDoc = { |
||
| 26 | type: 'doc', |
||
| 27 | content: selectedContent || [{ type: 'paragraph' }], |
||
| 28 | }; |
||
| 29 | try { |
||
| 30 | footnoteSchema.nodeFromJSON(footnoteDoc); |
||
| 31 | } catch (e) { |
||
| 32 | return false; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (dispatch) { |
||
| 36 | const footnoteNode = schema.nodes.footnote.create({ contentJSON: JSON.stringify(footnoteDoc) }); |
||
| 37 | dispatch(state.tr.replaceSelectionWith(footnoteNode)); |
||
| 38 | } |
||
| 39 | |||
| 40 | return true; |
||
| 41 | }, |
||
| 42 | icon: svgIcon('note-plus-outline'), |
||
| 43 | label: 'Add a footnote', |
||
| 44 | }); |
||
| 45 | } |
||
| 46 | } |
||
| 47 |